package com.spring.sunflower.util;

import android.app.Activity;
import android.content.Context;

import com.blankj.utilcode.util.SPUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.spring.sunflower.App;
import com.spring.sunflower.base.BaseApplication;
import com.spring.sunflower.bean.IMBaseBean;
import com.spring.sunflower.bean.LiveApplyChatBean;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.conversation.ChatVideoActivity;
import com.spring.sunflower.dialog.ChatSendGiftDialogUtils;
import com.spring.sunflower.dialog.DialogManager;
import com.spring.sunflower.dialog.LuckyMatchDialogUtils;
import com.spring.sunflower.dialog.TaskFinishDialogUtils;
import com.spring.sunflower.event.AccountFrozenEvent;
import com.spring.sunflower.event.CallRequestCancelEvent;
import com.spring.sunflower.event.ChatSendGiftEvent;
import com.spring.sunflower.event.GiftSendEvent;
import com.spring.sunflower.event.JoinRoomEvent;
import com.spring.sunflower.event.LuckyMatchEvent;
import com.spring.sunflower.event.NeedChargeEvent;
import com.spring.sunflower.event.RoomClosedEvent;
import com.spring.sunflower.event.TaskFinishEvent;
import com.spring.sunflower.event.VideoCallOverEvent;
import com.tencent.imsdk.v2.V2TIMCustomElem;
import com.tencent.imsdk.v2.V2TIMMessage;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONObject;

import java.lang.reflect.Type;

public class IMParseUtils {
    private static IMParseUtils instance;

    private String CHANNEL_ID = "Happy";

    private String CHANNEL_NAME = "Happy";

    private Context context = BaseApplication.getContext();

    private Gson gson = new Gson();

    private Type type;
    private String sender;

    public static IMParseUtils getInstance() {
        if (instance == null)
            instance = new IMParseUtils();
        return instance;
    }

    public void parseMessage(V2TIMMessage msg, Context context) {
        String userID = SPUtils.getInstance().getString(Constant.KEY_USERID, "");
        boolean ring = SPUtils.getInstance().getBoolean(Constant.KEY_CHAT_RING, true);
        boolean vibrate = SPUtils.getInstance().getBoolean(Constant.KEY_CHAT_VIBRATE, false);
        IMBaseBean imBaseBean;
        V2TIMCustomElem customElem = msg.getCustomElem();
        if (customElem == null) return;
        String data = new String(customElem.getData());
        LogUtils.INSTANCE.logD("ChatVideoActivity", "data=" + data);
        try {
            JSONObject object = new JSONObject(data);
            if (object.has("body")) {
                String type = object.getString("type");
                JSONObject body = object.getJSONObject("body");
                switch (type) {
                    case "1"://礼物
                        imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<GiftSendEvent>>() {
                        }).getType());
                        EventBus.getDefault().post(imBaseBean.getBody());
                        GiftSendEvent giftSendEvent = (GiftSendEvent) imBaseBean.getBody();
                        sender = giftSendEvent.getSender();
//                        if (!Constant.isInCall&&!sender.equals(userID)) {
//                            CallRingtoneUtils.getInstance(context).playMessageNotification();
//                        }
                        break;
                    case "2"://封号
                    case "3"://禁用账号
                        imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<AccountFrozenEvent>>() {
                        }).getType());
                        EventBus.getDefault().post(imBaseBean.getBody());
                        if (!Constant.isInCall || !msg.isSelf()) {
                            CallRingtoneUtils.getInstance(context).playMessageNotification();
                        }
                        break;
                    case "4"://收到视频邀请
                        imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<LiveApplyChatBean.Datas>>() {
                        }).getType());
                        ChatVideoActivity.onCallReceive(context, (LiveApplyChatBean.Datas) imBaseBean.getBody());
                        break;
                    case "5"://对方接受视频邀请
                        imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<JoinRoomEvent>>() {
                        }).getType());
                        EventBus.getDefault().post(imBaseBean.getBody());
                        break;
                    case "6"://用户取消-视频聊天申请
                    case "7"://主播取消视频邀请 data: {"body":{"anchorId":98263},"type":"7"}
                        imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<CallRequestCancelEvent>>() {
                        }).getType());
                        EventBus.getDefault().post(imBaseBean.getBody());
                        break;
                    case "8"://余额不足一分钟
                        imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<NeedChargeEvent>>() {
                        }).getType());
                        EventBus.getDefault().post(imBaseBean.getBody());
//                        Activity currentActivity = ((BaseApplication) context).getCurrentActivity();
//                        NoticeDialogUtils.getInstance().show(currentActivity,"余额提醒", "当前余额不足1分钟");
                        break;
                    case "9"://视频房间已关闭
                        imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<RoomClosedEvent>>() {
                        }).getType());
                        EventBus.getDefault().post(imBaseBean.getBody());
                        break;
                    case "10":
                        break;
                    case "11":
                        break;
                    case "12":
                        break;
                    case "13":
                        break;
                    case "14":
                        break;
                    case "15":
                        break;
                    case "16":
                        break;
                    case "17":
                        break;
                    case "18":
                        break;
                    case "24"://任务完成
                        imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<TaskFinishEvent>>() {
                        }).getType());
                        TaskFinishEvent taskFinishEvent = (TaskFinishEvent) imBaseBean.getBody();
//                        EventBus.getDefault().post(imBaseBean.getBody());
//                        ToastUtils.showShort("完成任务" + taskFinishEvent.getAward());
                        Activity activity = ((BaseApplication) context).getCurrentActivity();
                        TaskFinishDialogUtils.getInstance().show(activity, taskFinishEvent.getAward());
                        break;
                    case "25":
                        if (Constant.shouldShowLuckyMatchDialog) {
                            imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<LuckyMatchEvent>>() {
                            }).getType());
                            LuckyMatchEvent luckyMatchEvent = (LuckyMatchEvent) imBaseBean.getBody();
                            Activity activityC = ((BaseApplication) context).getCurrentActivity();
                            DialogManager.getInstance().showLuckyMatchDialog(activityC, luckyMatchEvent, "25");
                        }
                        break;
                    case "26"://视频通话未接通的提示，添加到拨打方本地
                        imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<VideoCallOverEvent>>() {
                        }).getType());
                        EventBus.getDefault().post(imBaseBean.getBody());
                        break;
                    case "28":
                        if (!AppUtils.isVerify(App.getContext()) && !Constant.isInCall && Constant.shouldShowCallDialog) {
                            imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<LiveApplyChatBean.Datas>>() {
                            }).getType());
                            ChatVideoActivity.onCallReceiveShow(context, (LiveApplyChatBean.Datas) imBaseBean.getBody());
                        }
                        break;
                    case "29":
                        if (!AppUtils.isVerify(App.getContext()) && !Constant.isInCall) {
                            imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<ChatSendGiftEvent>>() {
                            }).getType());
                            Activity activity2 = ((BaseApplication) context).getCurrentActivity();
                            ChatSendGiftDialogUtils.getInstance().show(activity2, (ChatSendGiftEvent) imBaseBean.getBody());
                        }
                        break;
                    case "30":
                        if (Constant.shouldShowLuckyMatchDialog && !AppUtils.isVerify(App.getContext())) {
                            imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<LuckyMatchEvent>>() {
                            }).getType());
                            LuckyMatchEvent luckyMatchEvent = (LuckyMatchEvent) imBaseBean.getBody();
                            Activity activityC = ((BaseApplication) context).getCurrentActivity();
                            DialogManager.getInstance().showLuckyMatchDialog(activityC, luckyMatchEvent, "30");
                        }
                        break;
                    default:
                        break;
                }
            } else {
                if (!Constant.isInCall && !msg.isSelf()) {
                    if (ring) {
                        if(userID.equals("10001")){
                            CallRingtoneUtils.getInstance(context).playCallRingtone();
                        }else{
                            CallRingtoneUtils.getInstance(context).playMessageNotification();
                        }
                    }
                    if (vibrate) {
//                        Activity activity = ((BaseApplication) context).getCurrentActivity();
//                        if (activity != null) {
//                        }
                        VibrateUtils.getInstance(context).playVibrateWithMessageNotification();
                    }
                }
                LogUtils.INSTANCE.logI("IM", "else");
                EventBus.getDefault().post(msg);
            }
        } catch (Exception e) {
            LogUtils.INSTANCE.logI("IM", e.getMessage());
        }
//        Type type1;
//        IMBaseBean iMBaseBean;

//        switch (b) {
//            case 11:
//            case 12:
////        type1 = (new TypeToken<IMBaseBean<ForChatUserBean>>() {
////
////          }).getType();
////        this.type = type1;
////        iMBaseBean = (IMBaseBean)this.gson.fromJson(paramString, type1);
////        EventBus.getDefault().post(iMBaseBean);
//                return;
//            case 10:
////        type1 = (new TypeToken<IMBaseBean<ForChatAmountChangedEvent>>() {
////
////          }).getType();
////        this.type = type1;
////        iMBaseBean = (IMBaseBean)this.gson.fromJson(iMBaseBean, type1);
////        EventBus.getDefault().post(iMBaseBean.getBody());
//                return;
//            case 9:
////        type1 = (new TypeToken<IMBaseBean<RoomClosedEvent>>() {
////
////          }).getType();
////        this.type = type1;
////        iMBaseBean = (IMBaseBean)this.gson.fromJson(iMBaseBean, type1);
////        EventBus.getDefault().post(iMBaseBean.getBody());
//                return;
//            case 8:
////        type1 = (new TypeToken<IMBaseBean<CoinLackEvent>>() {
////
////          }).getType();
////        this.type = type1;
////        iMBaseBean = (IMBaseBean)this.gson.fromJson(iMBaseBean, type1);
////        EventBus.getDefault().post(iMBaseBean.getBody());
//                return;
//            case 7:
////        type1 = (new TypeToken<IMBaseBean<JoinRoomEvent>>() {
////
////          }).getType();
////        this.type = type1;
//        iMBaseBean = (IMBaseBean)this.gson.fromJson(iMBaseBean, type1);
////        EventBus.getDefault().post(iMBaseBean.getBody());
//                return;
//            case 6:
////        type1 = (new TypeToken<IMBaseBean<OnlineAnchorRecommendBean>>() {
////
////          }).getType();
////        this.type = type1;
////        iMBaseBean = (IMBaseBean)this.gson.fromJson(iMBaseBean, type1);
////        EventBus.getDefault().post(iMBaseBean.getBody());
//                return;
//            case 4:
//            case 5:
////        type1 = (new TypeToken<IMBaseBean<CallReqeustCancelEvent>>() {
////
////          }).getType();
////        this.type = type1;
////        iMBaseBean = (IMBaseBean)this.gson.fromJson(iMBaseBean, type1);
////        EventBus.getDefault().post(iMBaseBean.getBody());
//                return;
//            case 3:
////        type1 = (new TypeToken<IMBaseBean<VideoCallReqeustBean>>() {
////
////          }).getType();
////        this.type = type1;
////        iMBaseBean = (IMBaseBean)this.gson.fromJson(iMBaseBean, type1);
////        VideoCallReceiveActivity.startVideoReceiveActivity(this.context, (VideoCallReqeustBean)iMBaseBean.getBody());
//                return;
//            case 1:
//            case 2:
////        type1 = (new TypeToken<IMBaseBean<AccountFrozenEvent>>() {
////
////          }).getType();
////        this.type = type1;
////        iMBaseBean = (IMBaseBean)this.gson.fromJson(iMBaseBean, type1);
////        EventBus.getDefault().postSticky(iMBaseBean.getBody());
////        LoginActivity.relogin(MyApplication.getApplication());
//                return;
//            case 0:
////        type1 = (new TypeToken<IMBaseBean<GiftSendEvent>>() {
////
////          }).getType();
////        this.type = type1;
////        iMBaseBean = (IMBaseBean)this.gson.fromJson(iMBaseBean, type1);
////        EventBus.getDefault().post(iMBaseBean.getBody());
//                return;
//        }
    }

//    private <T>void handleDataWithEvent(String data, Object o) {
//        imBaseBean = gson.fromJson(data, (new TypeToken<IMBaseBean<GiftSendEvent>>() {
//        }).getType());
//        EventBus.getDefault().post(imBaseBean.getBody());
//    }
}